package game.server;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.Socket;

public class SocketClientDemo {

	public static void main(String[] args) {
		String host = "localhost";
		int port = 4700;
		String sendMsg = "send data from client";
		connect(host, port, sendMsg.getBytes());
	}

	private static void connect(String host, int port, byte[] sendMsg) {
		Socket socket = null;
		try {
			socket = new Socket(host, port);

			InputStream in = socket.getInputStream();
			OutputStream out = socket.getOutputStream();

			out.write(sendMsg);

			int bytesRcvd;
			while (-1 == (bytesRcvd = in.read(sendMsg))) {

			}

			System.out.println("接收到的数据：" + new String(sendMsg));
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			try {
				if (socket != null) {
					socket.close();
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
		}

	}

}
